const puppeteer = require('puppeteer')
const fs = require('fs')
const xlsx = require('node-xlsx')

let searchText = ['骨窟寺', '明洞天主教堂', '埃菲尔铁塔']

let scrape = async (text) => {
    const browser = await puppeteer.launch({headless: false})
    const page = await browser.newPage()

    await page.goto('https://baike.baidu.com/item/' + text)

    const result = await page.evaluate(() => {
        let _obj = {},
            i = 0
        _obj.title = document.querySelector('.lemmaWgt-lemmaTitle-title h1').innerText
       
        let elements = document.querySelectorAll('body > div.body-wrapper > div.content-wrapper > div > div.main-content > div.para')

        for (var ele of elements){
            i++
            if (i == 1) {
                _obj.des1 = ele.innerText.replace(/\n|\r|\t/g,"")
            }else{
                _obj.des2 += ele.innerText.replace(/\n|\r|\t/g,"")
            }
        }

        return _obj
    })

    browser.close()
    return result
}


let allData = []
searchText.forEach((item, index) => {
    scrape(item).then((value) => {
        allData[index] = value
        if (index == searchText.length - 1) {
            fs.writeFileSync( 'ceshi.json', JSON.stringify(allData), err => {
                if (err) throw err
            })
        }
    }).catch(err => {
        console.log(err)
    })
})



